import 'package:dailoz/dio/interceptor/dio_interceptor.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';

class HttpUtil {
  static HttpUtil? instance;

  Dio? dio;
  BaseOptions? options;

  CancelToken cancelToken = CancelToken();

  static HttpUtil getInstance() {
    instance ??= HttpUtil();
    return instance!;
  }

  HttpUtil() {
    options = BaseOptions(
      // baseUrl: "http://192.168.0.101:8083/api",
      // baseUrl: 'http://192.168.0.106:8081/dailoz_war/api',
      // baseUrl: 'http://114.132.73.2:8090/dailoz_war/api',
      // baseUrl: 'http://192.168.31.7:8081/dailoz_war/api',
      baseUrl: "http://47.97.11.204:8083/api",
      connectTimeout: const Duration(seconds: 10),
      receiveTimeout: const Duration(seconds: 5),
      headers: {
        "version": "1.0.0",
      },
    );

    dio = Dio(options);
    dio?.interceptors.add(DioInterceptor());
    JPush().setBadge(0);
  }

  // get请求
  get(url, {data, cancelToken}) async {
    final prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString("user-token");
    final options = Options(headers: {"token": token});
    try {
      Response? response;
      response = await dio!.get(url, queryParameters: data, options: options, cancelToken: cancelToken);
      if (kDebugMode) {
        print("get success---------${response.statusCode}");
      }
      if (kDebugMode) {
        print("get success---------${response.data}");
      }
      return response.data;
    } catch (e) {
      if (kDebugMode) {
        print('something really unknown $e');
      }
    }
  }

  // post请求
  post(url, data, {options, cancelToken}) async {
    final prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString("user-token");
    try {
      Response? response;
      response = await dio!.post(
        url,
        data: data,
        options: Options(headers: {"token": token}),
        cancelToken: cancelToken,
      );
      if (kDebugMode) {
        print("post success---------${response.statusCode}");
      }
      if (kDebugMode) {
        print("post success---------${response.data}");
      }
      return response.data;
    } catch (e) {
      print("'something really unknown $e'");
    }
  }

  // delete请求
  delete(url, queryParameter) async {
    final prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString("user-token");
    try {
      Response? response;
      response = await dio!.delete(url, queryParameters: queryParameter, options: Options(headers: {"token": token}));
      if (kDebugMode) {
        print("delete success---------${response.statusCode}");
      }
      if (kDebugMode) {
        print("delete success---------${response.data}");
      }
      return response.data;
    } catch (e) {
      print("something really unknown $e");
    }
  }

  // put请求
  put(url, data) async {
    final prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString("user-token");
    try {
      Response response;
      response = await dio!.put(url, data: data, options: Options(headers: {"token": token}));
      if (kDebugMode) {
        print("put success---------${response.statusCode}");
      }
      if (kDebugMode) {
        print("put success---------${response.data}");
      }
      return response.data;
    } catch (e) {
      print("something really unknown $e");
    }
  }
}
